package com.yylending.las.repay.repaydetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.yyfax.common.recyclerview.adapter.CommonAdapter;
import com.yyfax.common.recyclerview.adapter.MultiItemTypeAdapter;
import com.yyfax.common.recyclerview.base.ViewHolder;
import com.yyfax.common.recyclerview.listener.OnRefreshListener;
import com.yyfax.common.recyclerview.view.IRecyclerView;
import com.yyfax.common.recyclerview.wrapper.StatesAdapter;
import com.yyfax.common.recyclerview.wrapper.WrapperAdapter;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.utils.FormatUtils;
import com.yylending.las.common.utils.TimeUtils;
import com.yylending.las.common.widgets.Divider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ying_he on 2017/7/8.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class SingleRepayDetailFragment extends BaseFragment implements StatesAdapter.RetryClickListener, OnRefreshListener {
	private static final String TAG = SingleRepayDetailFragment.class.getSimpleName();

	private static final String STATUS_HAS_REPAY = "已还";
	private static final String STATUS_TO_REPAY = "待还";
	private static final String STATUS_HAS_OVERDUE = "逾期";
	private static final String STATUS_IS_REPAYING = "还款中";

	// 当前期次状态:3,还款中
	private static final String CURRENT_TERM_STATUS_IS_REPAYING = "3";
	// 当前期次状态:2,待还
	private static final String CURRENT_TERM_STATUS_TO_REPAY = "2";
	// 当前期次状态:1,已还
	private static final String CURRENT_TERM_STATUS_HAS_REPAY = "1";
	// 当前期次状态:0,逾期
	private static final String CURRENT_TERM_STATUS_HAS_OVERDUE = "0";


	@BindView(R.id.divider_view)
	View mDividerView;
	@BindView(R.id.repay_tips_tv)
	TextView mRepayTipsTv;
	@BindView(R.id.repay_detail_rv)
	IRecyclerView mRepayDetailRv;

	Unbinder mUnbind;
	public WrapperAdapter mWrapAdapter;
	private MultiItemTypeAdapter<RepayDetailListModel.Repay.RepayDetail> mAdapter;
	private List<RepayDetailListModel.Repay.RepayDetail> mRepayList = new ArrayList<>();
	private String mLoanNo = "";

	public static SingleRepayDetailFragment newInstance(ArrayList<RepayDetailListModel.Repay.RepayDetail> detail, String loanNo) {
		SingleRepayDetailFragment fragment = new SingleRepayDetailFragment();
		Bundle args = new Bundle();
		args.putParcelableArrayList(RepayDetailActivity.KEY_BUNDLE_ARG_REPAY_DETAIL_LIST, detail);
		args.putString(RepayDetailActivity.KEY_INTENT_LOAN_NO, loanNo);
		fragment.setArguments(args);
		return fragment;
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_single_repay_detail, container, false);
		mUnbind = ButterKnife.bind(this, view);

		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initData();
		StatService.onEvent(getContext(), StatIDSet.EVENT_ID_CHECK_PAYMENT_DETAILS);
	}

	private void initData() {

		mRepayList = getArguments().getParcelableArrayList(RepayDetailActivity.KEY_BUNDLE_ARG_REPAY_DETAIL_LIST);
		mLoanNo = getArguments().getString(RepayDetailActivity.KEY_INTENT_LOAN_NO);

		mDividerView.setVisibility(View.VISIBLE);
		mRepayTipsTv.setVisibility(View.GONE);
		mRepayDetailRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
		mAdapter = new CommonAdapter<RepayDetailListModel.Repay.RepayDetail>(R.layout.adapter_item_single_repay_detail, mRepayList, mRepayDetailRv.getItemViewDelegateManager()) {
			@Override
			protected void convert(ViewHolder holder, RepayDetailListModel.Repay.RepayDetail repay, int position) {
				holder.setText(R.id.index_tv, repay.term);
				holder.setText(R.id.money_tv, FormatUtils.getAmountWithSymbol(repay.amount));
				holder.setText(R.id.date_tv, TimeUtils.formatDateTimeSlash(Long.valueOf(repay.repayDate)));

				String termStatus = "";
				int textColor = R.color.color_333333;
				TextView repayStatusTv = (TextView) holder.getView(R.id.repay_status_tv);
				switch (repay.termStatus) {
					case CURRENT_TERM_STATUS_TO_REPAY:
						termStatus = STATUS_TO_REPAY;
						break;
					case CURRENT_TERM_STATUS_HAS_REPAY:
						termStatus = STATUS_HAS_REPAY;
						break;
					case CURRENT_TERM_STATUS_HAS_OVERDUE:
						termStatus = STATUS_HAS_OVERDUE;
						mRepayTipsTv.setVisibility(View.VISIBLE);
						mDividerView.setVisibility(View.GONE);
						textColor = R.color.color_fff44d19;
						break;
					case CURRENT_TERM_STATUS_IS_REPAYING:
						termStatus = STATUS_IS_REPAYING;
						break;
					default:
						break;
				}
				repayStatusTv.setText(termStatus);
				repayStatusTv.setTextColor(getResources().getColor(textColor));
			}
		};
		mWrapAdapter = new WrapperAdapter(mAdapter, mRepayDetailRv, getActivity(), R.layout.state_view_no_data, R.layout.include_view_no_network);
		Divider divider = new Divider(DensityUtil.dip2px(getActivity(), 0.5f), getResources().getColor(R.color.color_f1f3f7), false,
				0, 0, 0, DensityUtil.dip2px(getActivity(), 0.5f), 0);
		mRepayDetailRv.addItemDecoration(divider);
		mWrapAdapter.setRetryClickListener(this);
		mRepayDetailRv.setAdapter(mWrapAdapter);
		mRepayDetailRv.setOnRefreshListener(this);
		mRepayDetailRv.setLoadMoreEnabled(false);
		mRepayDetailRv.setRefreshEnabled(false);

		if (mRepayList.size() <= 0) {
			// 当数据为空的时候,必须删除,否则会出现多余的空隙.
			mRepayDetailRv.removeItemDecoration(divider);
			mWrapAdapter.setState(StatesAdapter.STATE_EMPTY);
		}
	}

	@Override
	public void onRefresh() {
		requestData();
	}

	@Override
	public void onRetry() {
	}

	private void requestData() {
		RepayDetailActivity.RequestRepayDetailListEvent event = new RepayDetailActivity.RequestRepayDetailListEvent();
		event.loanNo = mLoanNo;
		event.type = "10";
		EventAgent.post(event);
	}

	public void setData(ArrayList<RepayDetailListModel.Repay.RepayDetail> detail) {
		mRepayDetailRv.stopRefreshing();
		mRepayList.clear();
		mRepayList.addAll(detail);
		mWrapAdapter.refreshData(mRepayList);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mUnbind.unbind();
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}
}
